# This file is part of BlackArch Linux ( https://www.blackarch.org/ ).
# See COPYING for license details.

pkgname=3proxy
pkgver=0.9.5
pkgrel=1
groups=('blackarch' 'blackarch-proxy')
pkgdesc='Tiny free proxy server.'
url='https://github.com/3proxy/3proxy'
license=('GPL')
arch=('x86_64' 'aarch64')
options=('!makeflags')
depends=('openssl')
source=("https://github.com/$pkgname/$pkgname/archive/refs/tags/$pkgver.tar.gz"
        "$pkgname.service"
        'sysusers.conf'
        'tmpfiles.conf')
sha512sums=('b668db6438da19ee2479228af744b9b3b9ff2c7e6d97aaf3b20737b165ab2baf656ac9ea92b6f1e169c428784c7d9e3e7eeb7c3effb1c0b77e5038319b6d2bcd'
            '3010f5626148459ba7cecc0929fb47b801a1aa70d3df02a7ddd46a147a7a5e811330d9bcaf5ea092c2f0ef7663c8d4d44d67236e9224362850f72a95817f5936'
            '0b98afd23e04f3a8fd249821b9a45bc8b6ad7f62d8b0119ed0cc7e8135675d33d39be7d18474330bf4e898b6d27a6e810f78ddf00163007e45bfa30c4bffbe2a'
            '5ef573d9e0e763bab5b67e9735843e2c7d931000b967aeac332f3cbb2e079c9ba282b1aa6d1adf5e23af6e97675ea83192d0c81734aa23743638e556f52e006f')

build() {
  cd "$pkgname-$pkgver"

  make prefix=/usr \
    DESTDIR="$pkgdir" \
    ETCDIR="/etc/$pkgname" \
    INITDIR=/etc/init.d \
    BINDIR=/usr/bin \
    -f Makefile.Linux
}

package() {
  cd "$pkgname-$pkgver"

  install -dm 755 "$pkgdir/usr/lib/systemd/system"

  make prefix=/usr \
    install DESTDIR="$pkgdir" \
    ETCDIR="$pkgdir/etc/$pkgname" \
    INITDIR="$pkgdir/etc/init.d" \
    LOGBASE="$pkgdir/var/log" \
    RUNBASE="$pkgdir/run" \
    -f Makefile.Linux

  install -Dm 644 -t "$pkgdir/etc/$pkgname/" "cfg/$pkgname.cfg.sample" \
    cfg/counters.sample

  # local files
  install -Dm 644 "$srcdir/$pkgname.service" \
    "$pkgdir/usr/lib/systemd/system/$pkgname.conf"
  install -Dm 644 "$srcdir/sysusers.conf" \
    "$pkgdir/usr/lib/sysusers.d/$pkgname.conf"
  install -Dm 644 "$srcdir/tmpfiles.conf" \
    "$pkgdir/usr/lib/tmpfiles.d/$pkgname.conf"

  find "$pkgdir/etc/$pkgname" -type f | xargs chmod 640

  # Fix conflict with extra/libproxy.
  mv "$pkgdir/usr/bin/proxy" "$pkgdir/usr/bin/$pkgname"
  mv "$pkgdir/usr/share/man/man8/proxy.8" \
    "$pkgdir/usr/share/man/man8/$pkgname-proxy.8"

  mv "$pkgdir/usr/local/$pkgname/libexec/"*.so "$pkgdir/usr/lib/"

  rm -rf "$pkgdir/usr/local" "$pkgdir/etc/$pkgname/conf" \
    "$pkgdir/var/log/$pkgname"

  mkdir -p "$pkgdir/var/log/$pkgname"
}

